package com.huazhi.wzsnw.controller;

import com.huazhi.wzsnw.exception.BusinessException;
import com.huazhi.wzsnw.exception.ErrorCode;
import com.huazhi.wzsnw.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class RouteController {

    @RequestMapping(value = "/getRoute", method = {RequestMethod.POST, RequestMethod.GET})
    public String getRoute(HttpServletRequest request,
                             HttpSession httpSession, HttpServletResponse response) {
        try {
            String type = request.getParameter("type");
            String url = request.getParameter("url");
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            session.setAttribute("user", (User)subject.getPrincipal());
            Date date=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            session.setAttribute("datetime",simpleDateFormat.format(date));
            session.setAttribute("year",sdf.format(date));
            return type + "/" + url;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.ERROR_SYSTEM_ROUTE);
        }
    }




}
